package com.woniuxy.huayoung.controller;

import com.woniuxy.huayoung.entity.*;
import com.woniuxy.huayoung.result.ResponseResult;
import com.woniuxy.huayoung.service.HotelService;
import com.woniuxy.huayoung.service.OrderService;
import com.woniuxy.huayoung.service.UserService;
import com.woniuxy.huayoung.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/hotel")
public class HotelController {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(HotelController.class);

    //对象注入
    @Resource
    private HotelService hotelService;

    @Resource
    private UserService userService;


    @GetMapping("/find") //String address,@RequestParam("name")
    public ResponseResult<List<Hotel>> find(String address){
        //假设查询出来

//        List<Hotel> hotels = Arrays.asList(
//                new Hotel(1,3,"七天",22.5,1,1010,"null","发射东风",null,"成都",0,1.2,0),
//                new Hotel(2,3,"希尔顿",252.5,1,10120,null,"发射东福冈软体风",null,"北京",0,15.2,0)
//        );

        List<Hotel> hotels = hotelService.findByAddress(address);


        //将数组进行封装返回
        ResponseResult<List<Hotel>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(hotels);
        //
        return responseResult;
        }

        @GetMapping("/findById")
        public ResponseResult<Hotel> findById(int id){

            Hotel hotel = hotelService.findById(id);
            //将数据进行封装返回
            ResponseResult<Hotel> responseResult = new ResponseResult<>();
            responseResult.setCode(200);    //状态码 参考http状态码
            responseResult.setMessage("success");
            responseResult.setData(hotel);
            //
            return responseResult;
        }
    @GetMapping("/findAllTheme")
    public ResponseResult<List<HotelTheme>> findAllTheme(){

        List<HotelTheme> hotels = hotelService.findAllTheme();

        //将数组进行封装返回
        ResponseResult<List<HotelTheme>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(hotels);
        //
        return responseResult;
    }
    @GetMapping("/findBySpe")
    public ResponseResult<List<HotelCity>> findBySpe(){

        List<HotelCity> hotelcitys = hotelService.findBySpe();

        //将数组进行封装返回
        ResponseResult<List<HotelCity>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(hotelcitys);
        //
        return responseResult;
    }
    @GetMapping("/findByCid")
    public ResponseResult<List<Hotel>> findByCid(int cid){

        List<Hotel> hotels = hotelService.findByCid(cid);

        //将数组进行封装返回
        ResponseResult<List<Hotel>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(hotels);
        //
        return responseResult;
    }
    @GetMapping("/findByTid")
    public ResponseResult<List<Hotel>> findByTid(int tid){

        List<Hotel> hotels = hotelService.findByTid(tid);

        //将数组进行封装返回
        ResponseResult<List<Hotel>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);    //状态码 参考http状态码
        responseResult.setMessage("success");
        responseResult.setData(hotels);
        //
        return responseResult;
    }

    @GetMapping("/HotelOrder")
    public ResponseResult<Object> hotelOrder(Order hotelOrder,HttpServletRequest request) {

        String token = request.getHeader("authorization");
        User user = userService.findByTelephone(JwtUtil.getAccount(token));
        String username = user.getAccount();
        Integer uid = user.getId();



        hotelOrder.setOrderno(UUID.randomUUID().toString().replaceAll("-",""));
        hotelOrder.setUid(uid);
        hotelOrder.setUsername(username);
        hotelOrder.setType(2);
        hotelOrder.setNum(1);


        hotelOrder.setStatus(0);
        hotelOrder.setPayType(1);
        hotelService.insertHotelOrder(hotelOrder);

        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMessage("下单成功");
        responseResult.setData(hotelOrder.getOrderno());

        return responseResult ;
    }
    @GetMapping("/DleByOrderno")
    public ResponseResult<Object> DleByOrderno(String orderno) {

        hotelService.delByOrder(orderno);

        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMessage("删除成功");
        return null ;
    }

    }

